package org.cloudlab.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.cloudlab.bean.Paper;
import org.cloudlab.bean.SysUser;
import org.cloudlab.common.CommonConst;
import org.cloudlab.common.YesNoState;
import org.cloudlab.common.RemoveState;
import org.cloudlab.mapper.PaperMapper;
import org.cloudlab.service.IPaperService;
import org.cloudlab.service.ISubRecordService;
import org.cloudlab.utils.JwtTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 文章服务层实现类
 *
 * @author Liu Yicong
 * @date 2023/9/7
 */
@Service
public class PaperServiceImpl extends ServiceImpl<PaperMapper, Paper> implements IPaperService {

	@Autowired
	@Lazy
	private PaperMapper paperMapper;

	@Autowired
	@Lazy
	private ISubRecordService subRecordService;

	@Autowired
	private JwtTokenUtil tokenUtil;

	@Override
	public Page<Paper> queryByPage(Page<Paper> page, Paper paper, String onlyOwn) {
		SysUser currentUser = tokenUtil.getCurrentUser();
		LambdaQueryWrapper<Paper> queryWrapper = new LambdaQueryWrapper<>();
		//按照缩写筛选
		if (StringUtils.isNotEmpty(paper.getAcronym())) {
			String lowerCase = paper.getAcronym().toLowerCase();
			queryWrapper.like(Paper::getAcronym, lowerCase);
		}

		//按照负责人筛选
		if (StringUtils.isNotEmpty(onlyOwn) && onlyOwn.equals(YesNoState.YES.getValue())) {
			queryWrapper.eq(Paper::getBelongUserId, currentUser.getId());
		}
		//未删除
		queryWrapper.eq(Paper::getIsRemove, RemoveState.NOT_REMOVE.getValue());

		return paperMapper.selectPage(page, queryWrapper);
	}

	@Override
	public List<Paper> queryByUserId() {
		SysUser currentUser = tokenUtil.getCurrentUser();
		Long userId = currentUser.getId();
		LambdaQueryWrapper<Paper> paperLambdaQueryWrapper = new LambdaQueryWrapper<>();
		//如果是管理员则可以查看所有的文章
		if (!Objects.equals(userId, CommonConst.ADMIN_ID)) {
			paperLambdaQueryWrapper = paperLambdaQueryWrapper.eq(Paper::getBelongUserId, userId);
		}
		return paperMapper.selectList(paperLambdaQueryWrapper);
	}

	@Override
	@Transactional(rollbackFor = RuntimeException.class)
	public void delete(Long paperId) {

		SysUser currentUser = tokenUtil.getCurrentUser();
		Paper paper = this.getById(paperId);
		Long userId = currentUser.getId();
		if (Objects.isNull(paper) || paper.getIsRemove() == RemoveState.REMOVED.getValue()) {
			throw new RuntimeException("删除失败, 要删除的数据可能已经删除了哦");
		}
		// 既不是负责人也不是管理员
		if (!userId.equals(paper.getBelongUserId())
				&& !userId.equals(CommonConst.ADMIN_ID)) {
			throw new RuntimeException("你没有删除文章信息的权限哦");
		}
		// 删除后将已经链接的投稿记录一并删除
		subRecordService.deleteByPaperId(paperId);


		paper.setUpdateTime(new Date());
		paper.setUpdateUserId(userId);
		paper.setIsRemove(RemoveState.REMOVED.getValue());

		this.updateById(paper);
	}

	@Override
	public void update(Paper paper) {
		SysUser currentUser = tokenUtil.getCurrentUser();
		// 既不是负责人也不是管理员
		if (!currentUser.getId().equals(paper.getBelongUserId())
				&& !currentUser.getId().equals(CommonConst.ADMIN_ID)) {
			throw new RuntimeException("你没有更新文章信息的权限哦");
		}
		paper.setUpdateUserId(currentUser.getId());
		paper.setUpdateTime(new Date());
		this.updateById(paper);
	}

	@Override
	public void add(Paper paper) {
		SysUser currentUser = tokenUtil.getCurrentUser();

		String acronym = paper.getAcronym().trim();

		long count = this.lambdaQuery()
				.eq(Paper::getIsRemove, RemoveState.NOT_REMOVE.getValue())
				.eq(Paper::getAcronym, acronym)
				.count();
		// 存在同名的情况
		if (count != 0) {
			throw new RuntimeException("添加失败, 看看是否存在期刊名相同的期刊吧");
		}

		paper.setCreateUserId(currentUser.getId());
		paper.setCreateTime(new Date());
		paper.setBelongUserId(currentUser.getId());
		paper.setBelongNickName(currentUser.getNickName());
		this.save(paper);
	}
}
